package game

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/game"
	gameReq "github.com/flipped-aurora/gin-vue-admin/server/model/game/request"
	gameRes "github.com/flipped-aurora/gin-vue-admin/server/model/game/response"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type BsProjectGameInfoApi struct{}

// CreateProjectGameInfo
// @Tags      BsProjectGameInfo
// @Summary   创建比赛
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      gameReq.ProjectGameInfoParams                  true  "api路径, api中文描述, api组, 方法"
// @Success   200   {object}  response.Response{msg=string}  "创建基础api"
// @Router    /projectGameInfo/createProjectGameInfo [post]
func (s *BsProjectGameInfoApi) CreateProjectGameInfo(c *gin.Context) {
	var req gameReq.ProjectGameInfoParams
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(req, utils.ApiVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	param := game.BsProjectGameInfo(req.BsProjectGameInfo)
	param.Status = 1
	err = projectGameInfoService.CreateProjectGameInfo(param)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
		return
	}
	for _, val := range req.UmpireIds {
		signUp := game.BsSignUp{
			UserId:      uint(val),
			GameinfoId:  req.GameInfoId,
			ProjectId:   req.ProjectId,
			AuthorityId: 9528,
		}
		signUpService.CreateSignUp(signUp)
	}

	response.OkWithMessage("创建成功", c)
}

// DeleteProjectGameInfo
// @Tags      BsProjectGameInfo
// @Summary   删除赛事信息
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      game.BsProjectGameInfo                  true  "Id"
// @Success   200   {object}  response.Response{msg=string}  "删除api"
// @Router    /projectGameInfo/deleteProjectGameInfo [delete]
func (s *BsProjectGameInfoApi) DeleteProjectGameInfo(c *gin.Context) {
	var param game.BsProjectGameInfo
	err := c.ShouldBindJSON(&param)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(param, utils.IdVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = projectGameInfoService.DeleteProjectGameInfo(param)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// GetProjectGameInfoList
// @Tags      BsProjectGameInfo
// @Summary   分页获取API列表
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      gameReq.ProjectGameInfoParams                               true  "分页获取赛事列表"
// @Success   200   {object}  response.Response{data=response.PageResult,msg=string}  "分页获取API列表,返回包括列表,总数,页码,每页数量"
// @Router    /projectGameInfo/getProjectGameInfoList [post]
func (s *BsProjectGameInfoApi) GetProjectGameInfoList(c *gin.Context) {
	var pageInfo gameReq.ProjectGameInfoParams
	err := c.ShouldBindJSON(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(pageInfo.PageInfo, utils.PageInfoVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	dataScope := common.DataScope{
		UserId:      utils.GetUserID(c),
		AuthorityId: utils.GetUserAuthorityId(c),
	}
	list, total, err := projectGameInfoService.GetProjectGameInfoList(pageInfo.BsProjectGameInfo, pageInfo.PageInfo, pageInfo.OrderKey, pageInfo.Desc, dataScope)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}

	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetProjectGameInfoById
// @Tags      BsProjectGameInfo
// @Summary   根据id获取赛事信息
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      request.GetById                                   true  "根据id获取赛事信息"
// @Success   200   {object}  response.Response{data=gameRes.BsProjectGameInfoResponse}  "根据id获取api,返回包括api详情"
// @Router    /projectGameInfo/getProjectGameInfoById [get]
func (s *BsProjectGameInfoApi) GetProjectGameInfoById(c *gin.Context) {
	var idInfo gameReq.ProjectGameInfoParams
	err := c.ShouldBindJSON(&idInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(idInfo, utils.IdVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	projectGameInfo, err := projectGameInfoService.GetProjectGameInfoById(int(idInfo.ProjectId), int(idInfo.GameInfoId))
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}

	// 获取裁判列表
	umpireList, _, err := signUpService.GetSignUpList(game.BsSignUp{
		GameinfoId:  idInfo.GameInfoId,
		ProjectId:   idInfo.ProjectId,
		AuthorityId: 9528,
	}, request.PageInfo{Page: 1, PageSize: 100}, "", false)
	if err != nil {
		global.GVA_LOG.Error("获取裁判列表失败!", zap.Error(err))
		response.FailWithMessage("获取裁判列表失败", c)
		return
	}
	realUmpireList := umpireList.([]game.BsSignUp)
	umpireIds := make([]int, 0)
	for i := 0; i < len(realUmpireList); i++ {
		umpireIds = append(umpireIds, int(realUmpireList[i].UserId))
	}
	projectGameInfo.UmpireIds = umpireIds
	response.OkWithDetailed(gameRes.BsProjectGameInfoResponse{ProjectGameInfo: projectGameInfo}, "获取成功", c)
}

// UpdateProjectGameInfo
// @Tags      BsProjectGameInfo
// @Summary   修改赛事信息
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      game.BsProjectGameInfo                  true  "api路径, api中文描述, api组, 方法"
// @Success   200   {object}  response.Response{msg=string}  "修改赛事信息"
// @Router    /projectGameInfo/updateProjectGameInfo [put]
func (s *BsProjectGameInfoApi) UpdateProjectGameInfo(c *gin.Context) {
	var param game.BsProjectGameInfo
	err := c.ShouldBindJSON(&param)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(param, utils.ApiVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	pg, err := projectGameInfoService.GetProjectGameInfoById(int(param.ProjectId), int(param.GameInfoId))
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	if param.Status > 0 {
		pg.Status = param.Status
	}
	if !param.ContestStartTime.IsZero() {
		pg.ContestStartTime = param.ContestStartTime
	}
	if !param.ContestEndTime.IsZero() {
		pg.ContestEndTime = param.ContestEndTime
	}
	if param.PlayerNum > 0 {
		pg.PlayerNum = param.PlayerNum
	}

	err = projectGameInfoService.UpdateProjectGameInfo(pg)
	if err != nil {
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
		response.FailWithMessage("修改失败", c)
		return
	}
	response.OkWithMessage("修改成功", c)
}
